package com.example.kotlinstudy

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.example.kotlinstudy.model.LoginRequest
import com.example.kotlinstudy.utils.RetrofitClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class HttpActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_http)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 1. 获取频道列表
        findViewById<Button>(R.id.getChannel).setOnClickListener {
            try {
                // 启动一个新的协程，用于处理获取频道列表的异步网络请求
                lifecycleScope.launch(context = Dispatchers.Main) {
                    // 使用 withContext 切换到 IO 线程，调用 Retrofit 接口获取频道列表数据
                    val res = withContext(Dispatchers.IO) {
                        // 调用 RetrofitClient 的 ApiService 获取频道数据
                        RetrofitClient.apiService.getChannels()
                    }
                    if (res.code == 10000) {
                        // 处理响应
                        val data = res.data?.channels
                        // 一行一条数据 设置给  findViewById<TextView>(R.id.channel).text
                        findViewById<TextView>(R.id.channel).text =
                            data?.joinToString(",,") { it.name }

                    } else {
                        // 处理响应
                        Toast.makeText(this@HttpActivity, "获取频道列表失败", Toast.LENGTH_SHORT)
                            .show()
                    }
                }
            } catch (e: Exception) {
                // 处理错误
                Toast.makeText(
                    this@HttpActivity,
                    "请求失败",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }

        // 2. 点击登录按钮时，调用登录接口，提交用户名：jackloverosema 密码：123456
        // 成功渲染token，失败提示异常
        findViewById<Button>(R.id.login).setOnClickListener {
//            try {
            lifecycleScope.launch(context = Dispatchers.Main) {
                val res = withContext(Dispatchers.IO) {
                    // 创建 LoginRequest 对象
                    val request = LoginRequest("jackloverosema", "123456")
                    RetrofitClient.apiService.login(request)

                }
                if (res.code == 10000) {
                    // 处理响应
                    val data = res.data
                    findViewById<TextView>(R.id.tv).text = data?.token
                } else {
                    // 处理响应
                    Toast.makeText(this@HttpActivity, "登录失败", Toast.LENGTH_SHORT)
                        .show()
                }
            }
//            }

        }
    }
}